﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using XCBPM.Mobile.Common;

namespace XCBPM.Mobile.DAL
{
    public class UserAndDepartmentDAO
    {
        public UserAndDepartmentDAO()
        {

        }
        /// <summary>
        /// 根据用户的姓名和AD帐号，进行模糊查询
        /// </summary>
        /// <param name="str">查询条件</param>
        /// <returns></returns>
        public DataTable GetUsersByCondition(string str)
        {
            DataTable dt = new DataTable();
            StringBuilder sqlStr=new StringBuilder();
            sqlStr.Append(
                "SELECT u.UserID,u.ChineseName+'（'+d.DepartmentName+'）' AS NameDepartment FROM Ult_UserExtend u");
            sqlStr.Append(" LEFT JOIN (SELECT DISTINCT UserID,DepartmentID FROM Ult_UserJob) j ON j.UserID=u.UserID ");
            sqlStr.Append(" LEFT JOIN Ult_Department d ON d.DepartmentID=j.DepartmentID ");
            sqlStr.Append(" WHERE (u.ADAccountName LIKE '%{0}%' OR u.ChineseName LIKE '%{0}%') ORDER BY u.ChineseName");
            DataSet ds = DBHelper.Query(string.Format(sqlStr.ToString(), str, str));
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            return dt;
        }
    }
}
